package com.example.testapp.activity.activity.study;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapp.R;
import com.example.testapp.activity.dabastate.SQLiteCityManager;

/**
 * Created by l on 2016/8/11.
 */
public class AddCityActivity extends AppCompatActivity {

    private GridView  mAddCityView;
    private static TextView  mCityTV;
    private boolean  ishas;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_addcity);
        initView();
    }

    private void initView() {
        mAddCityView = (GridView)findViewById(R.id.addcity_gridview);
        GridAddCityAdapter mAdapter = new  GridAddCityAdapter(this);
        mAddCityView.setAdapter(mAdapter);

        mAddCityView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCityTV = (TextView)view.findViewById(R.id.citytext);
                mCityTV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.city_checkbox_selected, 0);
                querydata(mCityTV.getText().toString());
                // 如果数据库中没有该城市，则添加到数据库。反之则提示。
                if(!ishas){
                    insertdata();
                    finish();
                }else{
                    Toast.makeText(AddCityActivity.this, "不可重复添加",  Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private SQLiteCityManager sqlite = new SQLiteCityManager(AddCityActivity.this, "weatherdb", null, 1);

    private void insertdata() {
        SQLiteDatabase db = sqlite.getReadableDatabase();

        ContentValues cv = new ContentValues();

        cv.put("cityname", mCityTV.getText().toString());
        cv.put("imageurl", "");
        cv.put("weather", "点击更新");
        cv.put("temp", "0℃");

        db.insert("coolWeather", "cityname", cv);
    }

    public void querydata(String str) {
        // 读写数据库
        SQLiteDatabase db = sqlite.getReadableDatabase();
        Cursor cursor = db.query("coolWeather", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String cityname = cursor.getString(cursor.getColumnIndex("cityname"));
            cityname = cityname.substring(0, 2);
            str = str.substring(0, 2);
            // 与当前按下的城市名做比较
            if (ishas = cityname.equals(str)) {
                return;
            }
        }
    }

    public static class GridAddCityAdapter extends BaseAdapter{

        private LayoutInflater mInflater;
        private SparseBooleanArray sba = new SparseBooleanArray();
        private String  nowcityname;
        private static final String[] cityname = new String[]{"北京", "上海", "广州","南京", "成都", "武汉", "杭州", "西安", "济南", "长春", "东莞",
                "沈阳", "天津", "哈尔滨", "长沙", "呼和浩特", "石家庄", "重庆", "无锡", "包头",
                "大连", "深圳", "福州", "海口", "乌鲁木齐", "兰州", "银川", "太原", "郑州",
                "合肥", "南昌", "南宁", "贵阳", "昆明", "拉萨", "西宁", "台北", "香港", "澳门" };

        public GridAddCityAdapter(Context context) {
           this.mInflater = LayoutInflater.from(context);

            SQLiteCityManager sqlite = new SQLiteCityManager(context, "weatherdb", null, 1);
            SQLiteDatabase db = sqlite.getReadableDatabase();
            Cursor cursor = db.query("coolWeather", null, null, null, null, null, null);
            while(cursor.moveToNext()){
                nowcityname = cursor.getString(cursor.getColumnIndex("cityname"));
                Log.i("TAG", nowcityname+"-->nowcityname");
                for(int i=0;i<cityname.length;i++){
                    if(nowcityname.equals(cityname[i])){
                        sba.put(i, true);
                    }
                }
            }
        }

        @Override
        public int getCount() {
            return cityname == null ? 0 : cityname.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.view_city_add_item, parent, false);
                holder.cityTv = (TextView)convertView.findViewById(R.id.citytext);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.cityTv.setText(cityname[position]);

            if(sba.get(position)){
                holder.cityTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.city_checkbox_selected, 0);
            }

            return convertView;
        }

        public class ViewHolder{
            public TextView cityTv;
        }
    }
}
